package com.fduss.artStore.servlet;

import com.fduss.artStore.entity.Customer;
import com.fduss.artStore.service.OrderService;
import com.fduss.artStore.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "BuyServlet", value = "/buy")
public class BuyServlet extends HttpServlet {
    private void doRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int artwork = 0;
        try {
            artwork = Integer.parseInt(request.getParameter("artwork"));
        }catch (Exception e) {
            return;
        }
        Customer customer = (Customer) request.getSession().getAttribute("user");
        if (customer == null) {
            response.getWriter().print(0);
            return;
        }
        OrderService orderService = new OrderServiceImpl();
        orderService.createOrder(artwork, customer.getCustomerId(), 1, 1, 1);
        response.getWriter().print(1);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doRequest(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doRequest(request, response);
    }
}
